home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / BBEdit / MacBob 1.0ß2 / Source / DialogUtilities.cp < prev    next >
Encoding:
Text File  |  1995-12-07  |  6.1 KB  |  284 lines  |  [TEXT/KAHL]

  1. /***
  2.   *
  3.   *    DialogUtilities.cp
  4.   *
  5.   *    Copyright © 1995, by Christopher E. Hyde.  All rights reserved.
  6.   *
  7.   ***/
  8.  
  9. #include <Icons.h>
  10. #include "DialogUtilities.h"
  11.  
  12.  
  13. Handle
  14. GetItemHandle (DialogPtr d, short item)
  15. {
  16.     Rect    rect;
  17.     Handle    tempH;
  18.     short    type;
  19.  
  20.     GetDialogItem(d, item, &type, &tempH, &rect);
  21.     return tempH;
  22. }
  23.  
  24.  
  25. void
  26. SetItemHandle (DialogPtr d, short item, Handle handle)
  27. {
  28.     Rect    rect;
  29.     Handle    tempH;
  30.     short    type;
  31.  
  32.     GetDialogItem(d, item, &type, &tempH, &rect);
  33.     SetDialogItem(d, item, type, handle, &rect);
  34. }
  35.  
  36.  
  37. void
  38. GetItemStr (DialogPtr d, short item, Str255 s)
  39. {
  40.     GetDialogItemText(GetItemHandle(d, item), s);
  41. }
  42.  
  43.  
  44. void
  45. SetItemStr (DialogPtr d, short item, ConstStr255Param s)
  46. {
  47.     SetDialogItemText(GetItemHandle(d, item), s);
  48. }
  49.  
  50.  
  51. short
  52. GetItemValue (DialogPtr d, short item)
  53. {
  54.     return GetControlValue(GetItemControl(d, item));
  55. }
  56.  
  57.  
  58. void
  59. SetItemValue (DialogPtr d, short item, short value)
  60. {
  61.     SetControlValue(GetItemControl(d, item), value);
  62. }
  63.  
  64.  
  65. bool
  66. GetItemHilite (DialogPtr d, short item)
  67. {
  68.     return (*GetItemControl(d, item))->contrlHilite != 0xFF;
  69. }
  70.  
  71.  
  72. // Temporarily highlight a push button
  73. void
  74. SetItemHilite (DialogPtr d, short item, bool flag)
  75. {
  76.     HiliteControl(GetItemControl(d, item), flag ? kControlButtonPart : kControlNoPart);
  77. }
  78.  
  79.  
  80. void
  81. SetItemEnable (DialogPtr d, short item, bool flag)
  82. {
  83.     HiliteControl(GetItemControl(d, item), flag ? kControlNoPart : kControlInactivePart);
  84. }
  85.  
  86.  
  87. // Toggle a checkbox or radio button's state and return its new state
  88. bool
  89. ToggleItem (DialogPtr d, short item)
  90. {
  91.     ControlHandle c = GetItemControl(d, item);
  92.     bool newState = (GetControlValue(c) == 0);
  93.     SetControlValue(c, newState);
  94.     return newState;
  95. }
  96.  
  97.  
  98. void
  99. ClickItem (ControlHandle c, short part)
  100. {
  101.     HiliteControl(c, part);
  102.     long finalTicks;
  103.     Delay(6, &finalTicks);
  104.     HiliteControl(c, kControlNoPart);
  105. }
  106.  
  107.  
  108. void
  109. ClickItem (DialogPtr d, short item, short part)
  110. {
  111.     ClickItem(GetItemControl(d, item), part);
  112. }
  113.  
  114.  
  115. #if 0
  116. // Given a dialog pointer and a button item number, this will cause the button to look as if it has been
  117. // clicked.  This is nice to do for the user if they type return or enter to select the default item.
  118. void
  119. SelectButton (DialogPtr d, short item)
  120. {
  121.     ClickItem(d, item, kControlButtonPart);
  122. }
  123.  
  124.  
  125. pascal void
  126. SetupUserItem (DialogPtr d, short item, UserItemProcPtr itemProc)
  127. {
  128.     short    type;
  129.     Handle    handle;
  130.     Rect    rect;
  131.  
  132.     GetDialogItem(d, item, &type, &handle, &rect);
  133.     SetDialogItem(d, item, type, Handle(itemProc), &rect);
  134. }
  135. #endif
  136.  
  137.  
  138. void
  139. ClickItem (DialogPtr d, short item)
  140. {
  141.     short    type;
  142.     Handle    handle;
  143.     Rect    rect;
  144.  
  145.     GetDialogItem(d, item, &type, &handle, &rect);
  146.  
  147.     short part;
  148.     switch (type) {
  149.         case kButtonDialogItem:            part = kControlButtonPart;        break;
  150.         case kCheckBoxDialogItem:        part = kControlCheckBoxPart;    break;
  151.         case kRadioButtonDialogItem:    part = kControlRadioButtonPart;    break;
  152.         default:                                                        return;
  153.     }
  154.     ClickItem(ControlHandle(handle), part);
  155. }
  156.  
  157.  
  158. void
  159. SetItemIcon (DialogPtr d, short item, short iconID)
  160. {
  161.     Handle icon = Get1Resource('ICON', iconID);
  162.     if (icon != nil) {
  163.         short    type;
  164.         Handle    handle;
  165.         Rect    rect;
  166.  
  167.         GetDialogItem(d, item, &type, &handle, &rect);
  168.         SetDialogItem(d, item, type, icon, &rect);
  169.  
  170.         PlotIcon(&rect, icon);
  171.     }
  172. }
  173.  
  174.  
  175. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  176. //    The following enables us to put key equivalent information in the resource fork, so that they are localizable.
  177. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  178.  
  179. enum {
  180.     kComd    =    1,
  181.     kShft    =    2,
  182.     kOptn    =    4,
  183.     kCtrl    =    8,
  184.     kShiftModifiers    =    8,
  185.     kNoModifiers    =    0,
  186.     kAllModifiers    =    cmdKey | shiftKey | optionKey | controlKey
  187. };
  188.  
  189. #if 0
  190. short pMods[] = {
  191.     kNoModifiers,                            cmdKey,
  192.     shiftKey,                                cmdKey | shiftKey,
  193.     optionKey,                                optionKey | cmdKey,
  194.     optionKey | shiftKey,                    optionKey | cmdKey | shiftKey,
  195.     controlKey,                                controlKey | cmdKey,
  196.     controlKey | shiftKey,                    controlKey | cmdKey | shiftKey,
  197.     optionKey | controlKey,                    optionKey | controlKey | cmdKey,
  198.     optionKey | controlKey | shiftKey,        optionKey | controlKey | cmdKey | shiftKey
  199. };
  200. #endif
  201.  
  202. struct TKeyEquiv {
  203.     uchar    fKey;            // Character code of keyboard key
  204.     UInt8    fItem;            // Number of dialog item to click
  205.     UInt8    fModsOn;        // Modifier keys that must be on (down)
  206.     UInt8    fModsOff;        // Modifier keys that must be off (up)
  207. //    Byte        fPart;            // The item part to click
  208. };
  209. typedef TKeyEquiv** TKeyEquivHandle;
  210.  
  211.  
  212. // Re-translate a keyDown event removing the effects of the control, option & command keys.
  213. // Call twice with SHIFT set to TRUE then FALSE to get values for both key legends.
  214. static long
  215. Modify (const EventRecord& event, bool shift)
  216. {
  217.         // Assemble new key code with no modifiers (initially).  Put virtual key code in low byte
  218.     short keyCode = (event.message & keyCodeMask) >> 8;
  219.     if (shift)
  220.         keyCode |= shiftKey;
  221.  
  222.         // get pointer to current 'KCHR'
  223.     void* KCHRPtr = (void*) GetScriptManagerVariable(smKCHRCache);
  224.  
  225.         // initialize KeyTranslate dead-key state see what ascii code is returned
  226.     UInt32 someState = 0;
  227.  
  228.         // look for returned values in both high and low word of result
  229.     return KeyTranslate(KCHRPtr, keyCode, &someState);
  230. }
  231.  
  232.  
  233. pascal bool
  234. KeyEquivFilter (DialogPtr dlog, EventRecord* event, short* item)
  235. {
  236.     if (event->what == keyDown) {
  237.         UInt8 modifiers = (event->modifiers & kAllModifiers) >> kShiftModifiers;
  238.         bool shift = false;
  239.         do {
  240.             shift = !shift;
  241.             uchar ch = Modify(*event, shift);
  242.  
  243.             if (ch == 0x0D || ch == 0x03) {            // If return or enter...
  244.                 *item = 1;
  245.                 return true;
  246.             }
  247.  
  248.             for (const TKeyEquiv* ke = *TKeyEquivHandle(DialogPeek(dlog)->window.refCon);
  249.                                     ke->fKey != 0; ++ke) {
  250.                 if (ch == ke->fKey && (modifiers & ke->fModsOn) == ke->fModsOn &&
  251.                                       (modifiers & ke->fModsOff) == 0) {
  252.                     ClickItem(dlog, *item = ke->fItem);
  253.                     return true;
  254.                 }
  255.             }
  256.         } while (shift);
  257.     }
  258.  
  259. #ifdef _BBEditExtention_
  260.     return BBEdit->StandardFilter(dlog, event, item);
  261. #else
  262.     return false;
  263. #endif
  264. }
  265.  
  266.  
  267. void
  268. InitKeyEquiv (DialogPtr dlog, short rsrcID)
  269. {
  270.     DialogPeek(dlog)->window.refCon = long(GetResource('DKey', rsrcID));
  271. }
  272.  
  273.  
  274. void
  275. EndKeyEquiv (DialogPtr dlog)
  276. {
  277.     Handle& ke = Handle(DialogPeek(dlog)->window.refCon);
  278.  
  279.     if (ke != nil) {
  280.         ReleaseResource(ke);
  281.         ke = nil;
  282.     }
  283. }
  284.